Introducing Classes

1. Creating a Class

A class is a blueprint for objects. It can have properties (fields), methods, and constructors. Classes support encapsulation, inheritance, and polymorphism.

    
    class Student {
        // Properties (instance variables)
        String name;
        int age;
    
        // Constructor
        Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        // Method to display details
        void displayDetails() {
            System.out.println("Name: " + name + ", Age: " + age);
        }
    }
    
    

2. Object Access Modifiers

Access modifiers define the scope of a class, method, or variable. Common types include:

    
    class Car {
        public String model;     // Accessible from anywhere
        private int year;        // Accessible only within this class
    
        public void setYear(int year) {
            this.year = year;    // Private variable accessed via method
        }
    
        public int getYear() {
            return year;
        }
    }
    
    

3. Method Overloading

Method overloading allows multiple methods with the same name but different parameters. This enhances readability and usability.

    
    class Calculator {
        // Method with two parameters
        int add(int a, int b) {
            return a + b;
        }
    
        // Overloaded method with three parameters
        int add(int a, int b, int c) {
            return a + b + c;
        }
    }
    
    

4. Garbage Collection

Garbage collection automatically deallocates memory for objects that are no longer referenced. Java’s garbage collector helps improve memory management.

    
    class GarbageExample {
        protected void finalize() {  // Called by Garbage Collector before object destruction
            System.out.println("Object is garbage collected");
        }
    }
    
    

5. 'this' Keyword

'this' refers to the current object instance. It distinguishes instance variables from method parameters.

    
    class Person {
        String name;
    
        Person(String name) {
            this.name = name;  // Refers to the instance variable
        }
    }
    
    

6. Static Keyword

The 'static' keyword allows variables, methods, and blocks to belong to the class rather than an instance. Static members are loaded only once at class loading time.

class MathUtils { static int square(int num) { // Static method return num * num; } static { // Static block System.out.println("Static block executed"); } }

7. Final Keyword

The 'final' keyword is used to declare constants, prevent method overriding, or inheritance. Final variables cannot be reassigned, final methods cannot be overridden, and final classes cannot be extended.

class FinalExample { final int CONSTANT = 100; // Constant variable final void display() { // Final method (cannot be overridden) System.out.println("Final method"); } }

8. Wrapper Classes

Wrapper classes convert primitive data types into objects. This helps in object manipulation and data conversion.

    
    class WrapperDemo {
        public static void main(String[] args) {
            int a = 10;
            Integer obj = Integer.valueOf(a); // Boxing
            int b = obj.intValue();          // Unboxing
        }
    }
    
    

9. String Class and Methods

The String class provides various methods for string manipulation. Strings are immutable in Java, meaning their content cannot be changed after creation.

    
    class StringDemo {
        public static void main(String[] args) {
            String str = "Hello World";
            System.out.println(str.length());     // Length of string
            System.out.println(str.toUpperCase()); // Convert to uppercase
            System.out.println(str.replace("World", "Java")); // Replace text
        }
    }
    
    

10. Viva/Interview Questions

'this' refers to the current object and is used to resolve conflicts between instance variables and parameters with the same name.
Method overloading is defining multiple methods with the same name but different parameter types or counts.
The 'final' keyword is used to declare constants, prevent method overriding, and prevent inheritance of classes.
Garbage collection is an automatic process where Java removes unused objects to free up memory.
A static block is executed once when the class is loaded into memory and is mainly used for static variable initialization.
Strings are immutable to enhance security, performance, and memory management.
'==' compares reference locations, whereas '.equals()' compares the actual content of objects.